package net.siufung.market.base.tenant.controller.regulation;

import lombok.AllArgsConstructor;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.VerifyRepeatDto;
import net.siufung.core.model.enums.MethodEventEnum;
import net.siufung.core.model.regulation.AbstractRegulation;
import net.siufung.market.base.tenant.mapper.entity.TenantQuota;
import net.siufung.market.base.tenant.service.ITenantQuotaService;

/**
 * @author 陈建峰
 * @since 2022/6/24 5:28 下午
 */
@AllArgsConstructor
public class TenantQuotaCreateRegulation extends AbstractRegulation<TenantQuota> {

    private static final long serialVersionUID = -5654728977291551278L;

    private final ITenantQuotaService tenantQuotaService;

    @Override
    public boolean isSatisfiedBy(TenantQuota tenantQuota) {
        VerifyRepeatDto<Integer> verifyRepeatDto = new VerifyRepeatDto<>();
        verifyRepeatDto.setMethodEventEnum(MethodEventEnum.ADD);
        verifyRepeatDto.setPk(null);
        verifyRepeatDto.setVerifyKeyValue(tenantQuota.getQuotaKey());
        if(!tenantQuotaService.checkTenantQuotaKeyNotRepeat(verifyRepeatDto)){
            throw new BizServiceException("租户配额Key不能重复！");
        }
        return Boolean.TRUE;
    }
}
